package com.czy.owner.ui.userinfo;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.login.LoginActivity;
import com.czy.owner.utils.ActivityHelper;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.widget.MyEditText;
import com.hyphenate.chat.EMClient;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.OnClick;

import static com.czy.owner.utils.PhoneUtils.ShowToastMessage;

/**
 * Created by wangyang on 2017/7/19 0019.
 * 修改手机号码
 */

public class UpdateUserPhone extends BaseActivity {
    @BindView(R.id.et_phone)
    MyEditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_update_user_phone;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("手机号码更换");
        getSubTitle().setText("完成");
    }

    @Override
    protected void updateViews() {

    }


    //获取验证码
    @OnClick(R.id.tv_get_code)
    public void tvGetGodeOnClick(View view) {
        String phone = etPhone.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            ShowToastMessage(UpdateUserPhone.this, getString(R.string.phone_error_null));
            etPhone.requestFocus();
            return;
        }

        if (!PhoneUtils.isMobileNO(phone)) {
            ShowToastMessage(UpdateUserPhone.this, getString(R.string.phone_error));
            etPhone.requestFocus();
            return;
        }
        mLoadView.ShowLoadView();
        RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.OWN_MODIFYPHONESMSCODE);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(UpdateUserPhone.this).getSession());
        params.addBodyParameter("phone", phone);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String data = PhoneUtils.checkResponseFlag(UpdateUserPhone.this, result);
                if (data != null) {
                    timer.start();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });
    }

    //   倒计时响应事件  timer.start();
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setClickable(false);// 设置不能点击
            tvGetCode.setText(millisUntilFinished / 1000 + getString(R.string.send_again));// 设置倒计时时间
            tvGetCode.setTextColor(getResources().getColor(R.color.appTipColor)); // 设置为灰色，这时是不能点击的
        }

        @Override
        public void onFinish() {
            tvGetCode.setText(getResources().getString(R.string.get_yanzhengma));
            tvGetCode.setClickable(true);// 重新获得点击
            tvGetCode.setTextColor(getResources().getColor(R.color.appThemeColor));

        }
    };

    @OnClick(R.id.toolbar_subtitle)
    public void complete(View view) {
        final String phone = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ShowToastMessage(UpdateUserPhone.this, getString(R.string.phone_error_null));
            etPhone.requestFocus();
            return;
        }

        if (!PhoneUtils.isMobileNO(phone)) {
            ShowToastMessage(UpdateUserPhone.this, getString(R.string.phone_error));
            etPhone.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(code)) {
            ShowToastMessage(UpdateUserPhone.this, "验证码不能为空");
            etCode.requestFocus();
            return;
        }
        mLoadView.ShowLoadView();
        RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.OWN_MODIFYPHONE);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(UpdateUserPhone.this).getSession());
        params.addBodyParameter("phone", phone);
        params.addBodyParameter("smsCode", code);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess==" + result);
                String data = PhoneUtils.checkResponseFlag(UpdateUserPhone.this, result);
                if (data != null) {
                    SharedPreferencesUtils.setParam(UpdateUserPhone.this, Constants.SHARED_PREFERENCES_KEY_PHONE, phone);
                    SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_STORAGE);
                    SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_SESSION_INFO_MODEL);
                    SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_USER_INFO_MODEL);
                    SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_OWNER_INFO_MODEL);
                    SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_CHAIN_STORES_MODEL);
                    SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID);
                    ActivityHelper.getInstance().finishAllActivity();
                    UserHelper.getInstance().setUserInfoModel(UpdateUserPhone.this,null);
                    UserHelper.getInstance().setSessionInfoModel(UpdateUserPhone.this,null);
                    UserHelper.getInstance().setOwnerInfoModel(UpdateUserPhone.this,null);
                    UserHelper.getInstance().setmChainStoresList(UpdateUserPhone.this,null);
                    UserHelper.getInstance().setIsLogined(false);
                    EMClient.getInstance().logout(true);
                    Intent ti = new Intent(UpdateUserPhone.this, LoginActivity.class);
                    startActivity(ti);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError==" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });

    }
}
